#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>

long showtime(long timeval);

int main(int argc,char *argv[]){
    if (argc<2) exit(1);
    struct stat buf;
    stat(argv[1],&buf);
    printf("  文件: %s\n",argv[1]);
    
    printf("  大小: %-16d块: %-11dIO 块: %-7d",buf.st_size,buf.st_blocks,buf.st_blksize);
    if(S_ISREG(buf.st_mode)) {char style[30]="普通文件";printf("%s\n",style);}                           //判断文件类型 
			else if(S_ISDIR(buf .st_mode)) {char style[30]="目录文件";printf("%s\n",style);}
				else if(S_ISCHR(buf.st_mode)) {char style[30]="字符设备文件";printf("%s\n",style);}
					else if(S_ISBLK(buf.st_mode)) {char style[30]="块设备文件";printf("%s\n",style);}
						else if(S_ISLNK(buf.st_mode)) {char style[30]="链接文件";printf("%s\n",style);}
							else if(S_ISFIFO(buf.st_mode)) {char style[30]="管理文件";printf("%s\n",style);}
								else if(S_ISSOCK(buf.st_mode)) {char style[30]="套接字文件";printf("%s\n",style);}
									else {char style[30]="未知文件";printf("%s\n",style);}

    printf("设备: %-5ld/%ldd      Inode: %-12d硬链接: %d\n",buf.st_rdev,buf.st_dev,buf.st_ino,buf.st_nlink);

    char str[10]={'-','-','-','-','-','-','-','-','-','\0'};//  判断权限
	int mode=buf.st_mode;
	str[0]=(mode&S_IRUSR)? 'r':'-';
	str[1]=(mode&S_IWUSR)? 'w':'-';
	str[2]=(mode&S_IXUSR)? 'x':'-';
	str[3]=(mode&S_IRGRP)? 'r':'-';
	str[4]=(mode&S_IWGRP)? 'w':'-';
	str[5]=(mode&S_IXGRP)? 'x':'-';
	str[6]=(mode&S_IROTH)? 'r':'-';
	str[7]=(mode&S_IWOTH)? 'w':'-';
	str[8]=(mode&S_IXOTH)? 'x':'-';

    struct passwd *pw = getpwuid(buf.st_uid);
    struct group  *gr = getgrgid(buf.st_gid);

    printf("权限: (%.4o/-%s)  Uid: (%5d/%8s)   Gid: (%5d/%8s)\n",buf.st_mode-32768,str,buf.st_uid,pw->pw_name,buf.st_gid,gr->gr_name);
    printf("环境: \n");
    printf("最近访问: ");
    showtime(buf.st_atime);
    printf("\n");
    printf("最近更改: ");
    showtime(buf.st_ctime);
    printf("\n");
    printf("最近改动: ");
    showtime(buf.st_mtime);
    printf("\n");
    // printf("%s\n",style);
}
long showtime(long timeval)
{

        struct tm *lccp;
        lccp=localtime(&timeval);
        printf("%d-%.2d-%.2d %.2d:%.2d:%.2d.          ",lccp->tm_year+1900,lccp->tm_mon+1,lccp->tm_mday,(lccp->tm_hour)%24,lccp->tm_min,lccp->tm_sec);
        time_t timeValue = 0;
        struct tm *p = NULL;
        time(&timeValue);
        p = gmtime(&timeValue);
        int i =p->tm_hour;
        p = localtime(&timeValue);
        int j =p->tm_hour;
        // p2 = localtime(&timeValue);
        printf("+%.2d00",((j-i)+24)%24);
}
